<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-form-item label="快递公司" prop="expressCompany">
          <el-select v-model="form.expressCompany" placeholder="请选择" class="col">
            <el-option
                v-for="item in expressCompanyList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="trackingNumber">
          <el-input v-model="form.trackingNumber" placeholder="快递单号" maxlength="30" class="col"></el-input>
        </el-form-item>
      </el-tabs>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:commodity:save']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {detail, getCategoryList, save} from "@/api/order";
import {getForm} from "@/utils/utils";

export default {
  props: {
    id: {
      default: null
    }
  },
  data() {
    return {
      activeName: "first",
      otherInfoVisible: false,
      form: {
        id: null,
        expressCompany: '',
        trackingNumber: '',
        status:''
      },
      rules: {
        expressCompany: [{required: true, message: '请选择快递公司', trigger: ['blur']}],
        trackingNumber: [{required: true, message: '请填写快递单号', trigger: ['blur']}],
      },
      roles: [],
      saveLoading: false,
      formLoading: false,
      uploads: {
        show: false,
        multiple: false,
      },
      imgUploads: {
        show: false,
        multiple: true,
      },
      expressCompanyList:[]
    }
  },
  mounted() {
    if (this.id) {
      this.getDetail();
      this.getExpressCompanyList();
    }
  },
  methods: {
    getExpressCompanyList(){
      getCategoryList().then(res =>{
        this.expressCompanyList = res
      })
    },
    handleClick(tab) {
      if (tab.name === 'second') {
        this.otherInfoVisible = true;
      } else {
        this.otherInfoVisible = false;
      }
    },
    getFile(fileList) {
      this.form.coverImg = fileList[0].url
      this.uploads.show = false
    },
    getImage(fileList) {
      this.form.file = fileList
      this.imgUploads.show = false
      let ids = []
      this.form.file.forEach(item => {
        ids.push(item.id)
      })
      this.form.fileId = ids.join(',')
    },
    handleChange(value) {
    },
    getDetail() {
      this.formLoading = true
      detail(this.id).then(res => {
        getForm(res, this.form);
        this.form.file = res.picUrl
        this.form.file = this.form.file.split(',')
        let list = []
        this.form.file.forEach(item => {
          list.push(Number(item))
        })
        this.form.file = list
      }).finally(() => {
        this.formLoading = false;
      })
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        this.form.status = 2;
        save(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideEditDialog')
    }
  }
}
</script>
<style lang="scss" scoped>
.col {
  width: 360px;
}

.img {
  width: 120px;
  height: 120px;
  margin-right: 10px;
}

.imgList {
  display: flex;
  flex-wrap: wrap;

  .img {
    width: 120px;
    height: 120px;
    margin-right: 10px;
  }
}
</style>
